import com.alibaba.fastjson.JSON;
import com.qf.pojo.User;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.Test;
import org.springframework.util.SerializationUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;

import java.nio.charset.StandardCharsets;

/**
 * @author YDW
 * projectName:4_01_jedis
 * time:2022/5/31 11:49
 * description:
 */
public class Demo1 {

    @Test
    public void set(){
        //1. 连接Redis
        Jedis jedis = new Jedis("120.48.49.214",6389);
        //2. 操作Redis - 因为Redis的命令是什么，Jedis的方法就是什么
//        jedis.set("name","王五");
//        jedis.incrBy("a",5);

        //3. 释放资源
        jedis.close();
    }

    @Test
    public void get(){
        //1. 连接Redis
        Jedis jedis = new Jedis("120.48.49.214",6389);
        //2. 操作Redis - 因为Redis的命令是什么，Jedis的方法就是什么
        String value = jedis.get("name");
        System.out.println(value);
        //3. 释放资源
        jedis.close();
    }

    @Test
    public void setObjectByByteArray(){
        Jedis jedis = new Jedis("120.48.49.214",6389);
        jedis.select(1);
        User user=new User();
        user.setId(2);
        user.setAge(22);
        user.setUserName("李四");
        user.setPassword("654321");
        user.setSex("女");
        byte[] value = SerializationUtils.serialize(user);
        byte[] key = SerializationUtils.serialize("user2");
        jedis.set(key,value);
        jedis.close();
    }

    @Test
    public void getObject(){
        Jedis jedis = new Jedis("120.48.49.214",6389);
        jedis.select(1);
        byte[] key = SerializationUtils.serialize("user2");
        byte[] bytes = jedis.get(key);
        User user = (User) SerializationUtils.deserialize(bytes);
        System.out.println(user);
        jedis.close();
    }
    @Test
    public void saveJson(){
        Jedis jedis = new Jedis("120.48.49.214",6389);
        jedis.select(1);
        User user=new User();
        user.setId(3);
        user.setAge(28);
        user.setUserName("王五");
        user.setPassword("456789");
        user.setSex("男");
        String s = JSON.toJSONString(user);
        jedis.set("user",s);
        jedis.close();
    }
    @Test
    public void getJson(){
        Jedis jedis = new Jedis("120.48.49.214",6389);
        jedis.select(1);
        String userJson = jedis.get("user");
        User user = JSON.parseObject(userJson, User.class);
        System.out.println(user);
        jedis.close();
    }

    @Test
    public void testJedisPool(){

        //配置连接池
        GenericObjectPoolConfig poolConfig=new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(100);  // 连接池中最大的活跃数
        poolConfig.setMaxIdle(10);   // 最大空闲数
        poolConfig.setMinIdle(5);   // 最小空闲数
        poolConfig.setMaxWaitMillis(3000);  // 当连接池空了之后,多久没获取到Jedis对象,就超时
        //创建连接池
        JedisPool jedisPool=new JedisPool(poolConfig,"120.48.49.214",6389);
        //从jedis连接池获取连接对象
        Jedis jedis = jedisPool.getResource();
    }

    /*
    * 测试管道
    * */
    @Test
    public void pipeLine(){
        Jedis jedis = new Jedis("120.48.49.214",6389);
        jedis.set("count","0");
        long start = System.currentTimeMillis();
//        for (int i=0;i<10000;i++){
//            jedis.incr("count"); //incr 给count变量+1
//        }
//        获取管道对象
        Pipeline pipelined = jedis.pipelined();
        for(int i=0;i<10000;i++){
            pipelined.incr("count");
        }
        long end = System.currentTimeMillis();
        System.out.println(end-start);
        jedis.close();

    }

}